angular.module('start.user.money.ctrl', [])

  .controller('MoneyCtrl', function($scope, $rootScope, $$UserMoneyService, $location, $ionicHistory, $ionicLoading) {
    console.log("MoneyCtrl");

    // $scope.$on("$ionicView.beforeEnter", function() { //每次进入页面前判断是否登录
    //   console.log("$ionicView.beforeEnter------MoneyCtrl");
    //   //判断登录代码
    //   // $ionicHistory.clearCache() ;
    // });

    $scope.moneyData = {
      money: 0,
      profit: 0,
      bills: []
    };


    $scope.pageData = {
      isloading: true,
      pageSize: 10,
      pageNum: 1,
      nomoreData: false
    };

    $scope.loadMore = function() {
      console.log("loadMore");
      if ($scope.pageData.nomoreData === false && !$scope.pageData.isloading) {
        $scope.getBilldatas();
      } else {
        $scope.$broadcast('scroll.infiniteScrollComplete');
      }
    };

    // 钱包信息
    $$UserMoneyService.getusermoneymsg()
      .then(function(res) {
        $scope.moneyData.money = res.data.money;
        $scope.moneyData.profit = res.data.profit;
      });

    // 账单信息
    $scope.getBilldatas = function() {
      $$UserMoneyService.getuserbillmsg($scope.pageData.pageNum, $scope.pageData.pageSize)
        .then(function(res) {
          if (res.errCode === 203) {
            if (res.data == undefined) {
              res.data = [];
            }
            $scope.moneyData.bills = $scope.moneyData.bills.concat(res.data);
            $scope.$broadcast('scroll.infiniteScrollComplete');
            // 如果没有更多数据 数据到底了
            $scope.pageData.pageNum++;
            if (res.data.length < $scope.pageData.pageSize) {
              $scope.pageData.nomoreData = true;
            } else {
              $scope.pageData.nomoreData = false;
            }
          } else {
            $ionicLoading.show({
              template: res.msg,
              noBackdrop: true,
              duration: 1500
            });
          }
          $scope.pageData.isloading = false;
        }, function() {
          console.info();
          $scope.pageData.isloading = false;
        });
    };

    $scope.getBilldatas();

    // 提现
    $scope.usergetmoney = function() {
      $location.path("getmoney");
    };

    // 充值
    $scope.useraddmoney = function() {
      $location.path("addmoney");
    };

  });
